local skynet = require "skynet"
local filelog = require "filelog"
local msghelper = require "agenthelper"
local msgproxy = require "msgproxy"
local playerdatadao = require "playerdatadao"
local processstate = require "processstate"
local table = table
require "enum"

local processing = processstate:new({timeout=4})
local  ApplyDisband = {}

--[[
//申请解散
message ApplyDisbandReq {
	optional Version version = 1;
	optional int32 id = 2;
	optional string roomsvr_id = 3; //房间服务器id
	optional int32  roomsvr_table_address = 4; //桌子的服务器地址
	optional int32  gameid = 5;		 		
}

message ApplyDisbandRes {
	optional int32 errcode = 1;
	optional string errcodedes = 2;
}
]]

function  ApplyDisband.process(session, source, fd, request)
	local responsemsg = {
		errcode = EErrCode.ERR_SUCCESS,
	}
	local server = msghelper:get_server()

	--检查当前登陆状态
	if not msghelper:is_login_success() then
		filelog.sys_warning("ApplyDisband.process invalid server state", server.state)
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "无效的请求！"
		msghelper:send_resmsgto_client(fd, "ApplyDisbandRes", responsemsg)		
		return
	end

	if processing:is_processing() then
		responsemsg.errcode = EErrCode.ERR_DEADING_LASTREQ
		responsemsg.errcodedes = "正在处理上一次请求！"
		msghelper:send_resmsgto_client(fd, "ApplyDisbandRes", responsemsg)		
		return
	end

	if server.gameid ~= 0 
		and server.gameid ~= request.gameid then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "当前请求不符合当前游戏！"
		msghelper:send_resmsgto_client(fd, "ApplyDisbandRes", responsemsg)		
		return
	end

	if server.roomsvr_id ~= "" 
		and server.roomsvr_table_id > 0 
		and server.roomsvr_table_id ~= request.id then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "当前请求不符合当前游戏！"
		msghelper:send_resmsgto_client(fd, "ApplyDisbandRes", responsemsg)		
		return		
	end
	request.rid = server.rid
	request.gatesvr_id = skynet.getenv("svr_id")
	request.agent_address = skynet.self()
	request.playerinfo = {
		rolename = server.info.rolename,
		logo = server.info.logo,
		sex = server.info.sex,
	}
	local refundinfo
	processing:set_process_state(true)
	responsemsg, refundinfo = msgproxy.sendrpc_reqmsgto_roomsvrd(request.gameid, nil, request.roomsvr_id, request.roomsvr_table_address, "applydisband", request)
	processing:set_process_state(false)

	if not msghelper:is_login_success() then
		return
	end

	if responsemsg ~= nil and responsemsg.errcode == EErrCode.ERR_SUCCESS and refundinfo ~= nil then
		local selfbuildtablecfgitem = msghelper:get_selfbuildtablecfgitem(refundinfo.gameid)
		if selfbuildtablecfgitem ~= nil then
			for _, gamesselectionitem in ipairs(selfbuildtablecfgitem.gamesselections) do
				if gamesselectionitem.retain_to_num == refundinfo.retain_to_num then
					if gamesselectionitem.gemstonenum > 0 then
						--扣除玩家身上的宝石
						msghelper:save_player_gemstone(server.rid, gamesselectionitem.gemstonenum, EReasonChangeCurrency.CHANGE_CURRENCY_DISBAND_TABLE, refundinfo.create_table_id)

						--通知client宝石改变
						msghelper:send_gemstonechange_notice()
					end
					break
				end
			end
		end
	end

	if responsemsg == nil then
		responsemsg = {
			errcode = EErrCode.ERR_INVALID_REQUEST,
			errcodedes = "无效的请求!",
		}
	end

	msghelper:send_resmsgto_client(fd, "ApplyDisbandRes", responsemsg)
end

return ApplyDisband

